package com.dongni.tools;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * json文件读取
 * @autor hongbing
 * @date 2018/10/12
 */
public class JsonDataUtil {

    /**
     * 从assets目录读取json文件
     * @param context
     * @param fileName
     * @return String
     */
    public String getJsonByAssets(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bf = null;
        AssetManager assetManager = null;
        try {
            assetManager = context.getAssets();
            bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bf != null) {
                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (assetManager != null) {
                assetManager.close();
            }
        }
        return stringBuilder.toString();
    }


}
